import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chip/config/Api.dart';
import 'package:flutter_chip/config/Http.dart';
import 'package:flutter_chip/utils/toast_utils.dart';
import 'package:flutter_chip/view/home/mcu_web.dart';

///文章分享页面
class ShareArticle extends StatefulWidget {
  @override
  _ShareArticleState createState() => _ShareArticleState();
}

class _ShareArticleState extends State<ShareArticle> {
  final TextEditingController _titleController = TextEditingController();
  final TextEditingController _linkController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        backgroundColor: Theme.of(context).canvasColor,
        title: Text('添加文章', style: TextStyle(fontSize: 18)),
        actions: <Widget>[
          FlatButton(
            onPressed: () {
              String title = _titleController.text;
              String link = _linkController.text;
              if (link.isEmpty) {
                Toast.show("请输入分享链接~");
                return;
              }
              Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                return WebInfoPage(title, link, type: "web");
              }));
            },
            child: Text(
              '预览',
              style:
                  TextStyle(fontSize: 16, color: Theme.of(context).accentColor),
            ),
            disabledTextColor: Color(0xff999999),
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text('文章标题'),
            _titleWidget(),
            Text('文章链接'),
            _linkWidget(),
            Container(
              alignment: Alignment.center,
              child: Text(
                "分享须知\n",
                style: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                  color: Color(0xff999999),
                ),
              ),
            ),
            _descWidget(),
            _submitWidget(),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _linkController.dispose();
    _titleController.dispose();
    super.dispose();
  }

  ///标题输入框
  Widget _titleWidget() {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10),
      constraints: BoxConstraints(
        maxHeight: 144.0,
        maxWidth: double.infinity,
        minHeight: 48.0,
        minWidth: double.infinity,
      ),
      decoration: BoxDecoration(
        color: Color(0xffeeeeee),
        borderRadius: BorderRadius.circular(6),
      ),
      padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 4),
      child: TextField(
        maxLines: null,
        controller: _titleController,
        maxLength: 50,
        keyboardType: TextInputType.multiline,
        decoration: InputDecoration.collapsed(
          hintText: "50字以内~",
          hintStyle: TextStyle(
            color: Color(0xffaaaaaa),
          ),
        ),
      ),
    );
  }

  ///链接输入框
  Widget _linkWidget() {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 20),
      constraints: BoxConstraints(
        maxHeight: 144.0,
        maxWidth: double.infinity,
        minHeight: 40.0,
        minWidth: double.infinity,
      ),
      decoration: BoxDecoration(
        color: Color(0xffeeeeee),
        borderRadius: BorderRadius.circular(6),
      ),
      padding: EdgeInsets.only(left: 16.0, right: 16.0),
      child: TextField(
        controller: _linkController,
        maxLines: null,
        keyboardType: TextInputType.multiline,
        decoration: InputDecoration.collapsed(
            hintText: "请输入分享链接",
            hintStyle: TextStyle(color: Color(0xffaaaaaa))),
      ),
    );
  }

  ///分享须知
  Widget _descWidget() {
    return Text(
      "1、只要是好文章都可进行分享，并不一定是原创\n2、CSDN,简书等官方博客站点直接通过，不需要审核\n3、无效分享尽快删除，否则可能对您的账号产生一定影响",
      style: TextStyle(
        fontSize: 12,
        fontWeight: FontWeight.w400,
        color: Color(0xff999999),
      ),
    );
  }

  ///提交按钮
  Widget _submitWidget() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 20),
      child: SizedBox(
        width: 160,
        child: RaisedButton(
          elevation: 6,
          onPressed: () {
            submit();
          },
          child: Text("分享"),
          textColor: Colors.white,
          color: Theme.of(context).accentColor,
          shape: StadiumBorder(
            side: BorderSide(color: Theme.of(context).accentColor),
          ),
        ),
      ),
    );
  }

  ///提交按钮
  void submit() {
    String title = _titleController.text;
    String link = _linkController.text;
    if (title.isEmpty || link.isEmpty) {
      Toast.show("标题或链接不能为空");
    }
    CancelFunc cancelFunc = Toast.loading("分享中...");
    Map<String, String> map = Map();
    map['content'] = "![$link";
    map['title'] = title;
    map['typeDesc'] = '推荐';
    Http.post(Api.URL_TOPIC_ADD, params: map, successCallBack: (data) {
      cancelFunc();
      Toast.show("分享成功");
      Navigator.of(context).pop();
    }, errorCallBack: (msg) {
      cancelFunc();
      Toast.show(msg);
    });
  }
}
